package com.netty4.http.xml;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;

import java.net.InetAddress;
import java.util.List;

import static io.netty.handler.codec.http.HttpUtil.setContentLength;

public class HttpXmlRequestEncoder extends  AbstractHttpXmlEncoder<HttpXmlRequest>{

    protected void encode(ChannelHandlerContext channelHandlerContext, HttpXmlRequest httpXmlRequest, List<Object> out) throws Exception {
        ByteBuf body = encode0(channelHandlerContext,httpXmlRequest.getBody());
        FullHttpRequest request = httpXmlRequest.getRequest();
        if (request == null) {
            request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,"/do",body);
            HttpHeaders headers = request.headers();
            headers.set(HttpHeaderNames.HOST, InetAddress.getLocalHost().getAddress());
            headers.set(HttpHeaderNames.CONNECTION,HttpHeaderValues.CLOSE);
            headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP.toString() + "," + HttpHeaderValues.DEFLATE.toString());
            headers.set(HttpHeaderNames.ACCEPT_CHARSET,"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            headers.set(HttpHeaderNames.ACCEPT_LANGUAGE,"zh");
            headers.set(HttpHeaderNames.ACCEPT,"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            setContentLength(request,body.readableBytes());
            out.add(request);
        }
    }
}
